package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.adapter.RecyclerViewBaseAdapter;
import com.example.zzl.model.bean.Home;
import com.example.zzl.utils.DisplayUtils;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/10.
 */

public class SpecialAdapter extends RecyclerViewBaseAdapter<Home.Special.ResultBean, SpecialAdapter.ViewHolder>{

    public SpecialAdapter(Context context, List<Home.Special.ResultBean> list) {
        super(context, list);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.e("SpecialAdapter", "onCreateViewHolder: " + viewType);
        ImageView imageView = new ImageView(context);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, DisplayUtils.dipTopx(140));
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return new ViewHolder(imageView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
//        Log.e("SpecialAdapter", "onBindViewHolder: " + position);
        super.onBindViewHolder(holder, position);
//        Log.e("SpecialAdapter", "onBindViewHolder: " + position + list);
        Home.Special.ResultBean resultBean = list.get(position);
        holder.itemView.setTag(resultBean.strSpecialImage);
        ((ImageView) holder.itemView).setImageBitmap(null);
        VolleySingleton.volleySingleton.getImageLoader().get(resultBean.strSpecialImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (holder.itemView.getTag().equals(response.getRequestUrl())) {
                    ((ImageView) holder.itemView).setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
